//
//  JMPaymentView.m
//  NewiOSProject
//
//  Created by JMMac on 16.1.24.
//  Copyright © 2024 范智渊. All rights reserved.
//

#import "JMPaymentView.h"

@interface JMPaymentView()
@property (nonatomic, strong)UILabel *titleLab;
@property (nonatomic, strong)UIImageView *bancosImgV;
@property (nonatomic, strong)UIImageView *oxxoImgV;

@property (nonatomic, strong)UIView *topLineV;
@property (nonatomic, strong)UIView *centerLineV;


@property (nonatomic, strong)UILabel *bancosLab;
@property (nonatomic, strong)UILabel *oxxoLab;

@property (nonatomic, strong)UIButton *bancosSelBtn;
@property (nonatomic, strong)UIButton *oxxoSelBtn;

@property (nonatomic, strong)UIView *bancosLineV;
@property (nonatomic, strong)UIView *oxxoLineV;

@property (nonatomic, strong)UIButton *bancosCover;
@property (nonatomic, strong)UIButton *oxxoCover;


//付款码数字，按钮 底部支付方式，以及引导语和协议按钮
@property (nonatomic, strong)UIView*botView;
//Pague mediante transferencia bancaria a la  cuenta bancaria designada
@property (nonatomic, strong)UILabel *tipsLab;
//二维码
@property (nonatomic, strong)UIImageView *qrcodeImgV;
//code码
@property (nonatomic, strong)UILabel *codeNumLab;
//STP OPM
@property (nonatomic, strong)UILabel *bankTypeLab;
//复制
@property (nonatomic, strong)UIView*coppyBtn;
//Como realizar el pago
@property (nonatomic, strong)UILabel *leadTitleLab;
//引导语html加载
@property (nonatomic, strong)WKWebView *leadwebView;
@end
@implementation JMPaymentView

//        ● 还款共有2种方式，bancos和Paynet。每种方式取一个，取不到的则不展示该还款方式。
//          ○ bancos方式：取repaymentCodeList字段列表中，repaymentBank不为"OXXO"的数据便是此还款方式的数据
//          ○ Paynet方式：先判断openPayCode是否为空，不为空则取此字段数据。若openPayCode为空，则取repaymentCodeList字段列表中repaymentBank为"OXXO"的数据
//        ● 固定文案区域：此区域文案为固定文案：Nuestra aplicación solamente recibe pagos mediante STP, Transfer o Paynet, por favor preste atención a la seguridad de sus fondos，其中STP, Transfer o Paynet需要变色显示



//        ● 还款方式图标：还款方式展示的图片种类共三种，openpay是一种，oxxo一种，剩下的都是bancos.还款方式最多展示两种
//        ● 固定文案区域：还款方式图标下方为固定提示文案，取UI上的文字
//        ● 付款码：取paymentCode字段。
//        ○ 如果是bancos方式，需要将repaymentBank展示出来
//        ○ 如果是Paynet方式，需要将barcodeUrlAlfa条形码和expireTime到期时间展示出来（此字段为空则不展示）
//        ○ 如果是OXXO方式，需要将barcodeUrlAlfa条形码展示出来（此字段为空则不展示）
//        ○ 点击Copy按钮：先复制付款码到粘贴板并提示【éxito】

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
       _topLineV =[JMControl addHorLineWithSpace:0 top:0 height:12 bgColor:Hex_Str_COLOR(@"#F6F9FE") toView:self];
        self.backgroundColor = WhiteColor();
        UILabel *soloLab = [JMControl createLabelWithFrame:(CGRectMake(0, 24, 150, 24)) Font:fontWithWeight(17, UIFontWeightMedium) Text:@"Elección del método de pago" color:Hex_Str_COLOR(@"#222222")];
        [self addSubview:soloLab];
        soloLab.backgroundColor = WhiteColor();
        soloLab.textAlignment = 1;
      
        
        
        UIView *lineV = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, 198, 2))];
        lineV.backgroundColor = Hex_Str_COLOR(@"#3C57F8");
        [self addSubview:lineV];
        [self sendSubviewToBack:lineV];
        lineV.center = soloLab.center;
        
        CGFloat w = CalculateLabelWidthWithLab(soloLab)+16;
        soloLab.width = w;
        soloLab.centerX = kAppW()/2;
        lineV.width = w+16+48;
        lineV.centerX = soloLab.centerX;
        
        _bancosImgV = [JMControl createImageViewWithFrame:(CGRectMake(24, soloLab.bottom+24, 44, 44)) ImageName:@"ICON／bancos"];
        [self addSubview:_bancosImgV];
        _bancosLab = [JMControl createLabelWithFrame:(CGRectMake(_bancosImgV.right+12, _bancosImgV.top, 100, 44)) Font:Font(15) Text:@"BAN" color:Text_COLOR_LEVEL1()];
        [self addSubview:_bancosLab];
        
        _bancosSelBtn = [JMControl createButtonWithFrame:(CGRectMake(kAppW()-24-24, 0, 24, 24)) ImageName:@"ICON／xuznze-1" Target:nil Action:nil font:nil Title:nil];
        [_bancosSelBtn setImage:ImageNamed(@"ICON／xuznze") forState:(UIControlStateSelected)];
        _bancosSelBtn.centerY = _bancosLab.centerY;
        [self addSubview:_bancosSelBtn];
        
        _bancosLineV = [JMControl addHorLineWithSpaceWithDefaultColor:24 top:_bancosImgV.bottom+8 height:1 toView:self];
        _bancosCover = [[UIButton alloc]initWithFrame:(CGRectMake(_bancosImgV.left, _bancosImgV.top, kAppW()-48, 44))];
        _bancosCover.tag = 1;
        [_bancosCover addTarget:self action:@selector(chooseBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:_bancosCover];
        
        
        _oxxoImgV = [JMControl createImageViewWithFrame:(CGRectMake(24, _bancosLineV.bottom+8, 44, 44)) ImageName:@"ICON／oxxo"];
        _oxxoImgV.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_oxxoImgV];
        _oxxoLab = [JMControl createLabelWithFrame:(CGRectMake(_bancosImgV.right+12, _oxxoImgV.top, 100, 44)) Font:Font(15) Text:@"OXXO" color:Text_COLOR_LEVEL1()];
        [self addSubview:_oxxoLab];
        _oxxoSelBtn = [JMControl createButtonWithFrame:(CGRectMake(kAppW()-24-24, 0, 24, 24)) ImageName:@"ICON／xuznze-1" Target:nil Action:nil font:nil Title:nil];
        _oxxoSelBtn.centerY = _oxxoLab.centerY;
        [_oxxoSelBtn setImage:ImageNamed(@"ICON／xuznze") forState:(UIControlStateSelected)];
        [self addSubview:_oxxoSelBtn];
        
        _oxxoLineV = [JMControl addHorLineWithSpaceWithDefaultColor:24 top:_oxxoImgV.bottom+8 height:1 toView:self];
        _oxxoCover = [[UIButton alloc]initWithFrame:(CGRectMake(_oxxoImgV.left, _oxxoImgV.top, kAppW()-48, 44))];
        [_oxxoCover addTarget:self action:@selector(chooseBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:_oxxoCover];
        _oxxoCover.tag = 2;
        _centerLineV =[JMControl addHorLineWithSpace:0 top:_oxxoLineV.bottom+12 height:12 bgColor:Hex_Str_COLOR(@"#F6F9FE") toView:self];
        self.height = _centerLineV.bottom;
    }
    return self;
}
-(void)chooseBtnClick:(UIButton *)sender{
    _paymentModel.payType = sender.tag;
    _paymentModel.currentModel = sender.tag==1?self.paymentModel.leftPayModel:self.paymentModel.rightModel;
    _bancosLineV.backgroundColor = sender.tag==1?Hex_Str_COLOR(@"#3C57F8"):mainSepartorColor();
    _oxxoLineV.backgroundColor = sender.tag>1?Hex_Str_COLOR(@"#3C57F8"):mainSepartorColor();
    _bancosSelBtn.selected = sender.tag==1;
    _oxxoSelBtn.selected = sender.tag>1;
    
    [self.botView removeAllSubviews];
    [self.botView removeFromSuperview];
    self.botView = nil;
    [self addSubview:self.botView];
    if ([self.delegate respondsToSelector:@selector(botViewUpdateHeight)]) {
        [self.delegate botViewUpdateHeight];
    }
  
}
-(void)setPaymentModel:(JMRepaymentModel *)paymentModel{
    _paymentModel = paymentModel;
    _paymentModel.currentModel = paymentModel.leftPayModel;
    _paymentModel.payType = paymentModel.leftPayModel.payType;
    _bancosLineV.backgroundColor = mainSchemeColor();
    
    if (paymentModel.openPayCode) {
        _oxxoCover.tag = 3;
        [_oxxoImgV setImage:ImageNamed(@"ICON ／Paynet")];
        _oxxoLab.text = @"Paynet";
    }
    [self addSubview:self.botView];
    self.height = self.botView.bottom;
  
   
}
-(UIView *)botView{
    if (!_botView) {
        _botView = [[UIView alloc]initWithFrame:(CGRectMake(0, _centerLineV.bottom, kAppW(), 1))];
        _tipsLab = [JMControl createLabelWithFrame:(CGRectMake(24,12, kAppW()-48, 1)) Font:fontWithWeight(12, UIFontWeightRegular) Text:@"" color:Text_COLOR_LEVEL2()];
        [_botView addSubview:_tipsLab];
        
       
        NSString *htmlString = @"";

        if (_paymentModel.payType ==1) {
            _tipsLab.text = @"Pague mediante transferencia bancaria a la  cuenta bancaria designada";
            htmlString  = _paymentModel.leftPayModel.repaymentLead;
            _leadTitleLab.text = @"Como realizar el pago";
            _bankTypeLab.text = [NSString stringWithFormat:@"%@:",[_paymentModel.leftPayModel.repaymentBank isOXXO]?@"":_paymentModel.leftPayModel.repaymentBank];
        }else{
            _tipsLab.text = @"Especifique el número de referencia del cajero en esta etiqueta para que pueda escribir directamente en la pantalla de ventas";
            htmlString  = _paymentModel.rightModel.repaymentLead;
            _leadTitleLab.text = @"OXXO";
            _bankTypeLab.text = [NSString stringWithFormat:@"%@:",[_paymentModel.leftPayModel.repaymentBank isOXXO]?@"":_paymentModel.rightModel.repaymentBank];

            if (_paymentModel.payType==3) {
                _leadTitleLab.text = @"Openpay";
            }
        }
        
        _tipsLab.height = CalculateLabelHeightWithLab(_tipsLab);
        _tipsLab.top = 12;
        
        _qrcodeImgV = [[UIImageView alloc]initWithFrame:(CGRectMake(0, _tipsLab.bottom+12, kAppW(), 60))];
        [_botView addSubview:_qrcodeImgV];
        _qrcodeImgV.contentMode = UIViewContentModeScaleAspectFit;
        _qrcodeImgV.hidden = YES;
        CGFloat top = _tipsLab.bottom+12;
        if (self.paymentModel.currentModel.barcodeUrlAlfa.length) {
            _qrcodeImgV.hidden = NO;
            
            [_qrcodeImgV sd_setImageWithURL:URLImageWithUrlStr(_paymentModel.currentModel.barcodeUrlAlfa)placeholderImage:nil];
            top = _qrcodeImgV.bottom+12;
        }
      
        _bankTypeLab = [JMControl createLabelWithFrame:(CGRectMake(_tipsLab.left, top, kAppW(), 21)) Font:fontWithWeight(13, UIFontWeightMedium) Text:@"STP" color:Text_COLOR_LEVEL2()];
        [_botView addSubview:_bankTypeLab];
        
      
        
        _codeNumLab = [JMControl createLabelWithFrame:(CGRectMake(_bankTypeLab.left, top, kAppW(), 24)) Font:fontWithWeight(15, UIFontWeightRegular) Text:@"" color:Text_COLOR_LEVEL1()];
        [_botView addSubview:_codeNumLab];
        _codeNumLab.centerY = _bankTypeLab.centerY;
        _codeNumLab.text = self.paymentModel.currentModel.paymentCode;
        if (self.paymentModel.currentModel.payType==3) {
            _bankTypeLab.hidden = YES;
            _codeNumLab.textAlignment = 0;
        }else{
            _bankTypeLab.hidden = NO;
            _codeNumLab.textAlignment = 1;
        }
        
        
        _coppyBtn = [JMControl createButtonWithFrame:(CGRectMake(kAppW()-24-50, 0, 50, 40)) ImageName:nil Target:self Action:@selector(copyBtnClick) font:Font(15) Title:@"Copy"];
        [_botView addSubview:_coppyBtn];
        _coppyBtn.centerY = _bankTypeLab.centerY;

        
        UIView *Line = [JMControl addHorLineWithSpace:24 top:_bankTypeLab.bottom+14 height:1 bgColor:Hex_Str_COLOR(@"#EBEBED") toView:_botView];
        
        _leadTitleLab = [JMControl createLabelWithFrame:(CGRectMake(24, Line.bottom+12, kAppW(), 17)) Font:Font(12) Text:@"" color:Hex_Str_COLOR(@"#666666")];
        [_botView addSubview:_leadTitleLab];
        
        _leadwebView = [[WKWebView alloc]initWithFrame:(CGRectMake(_bankTypeLab.left, _leadTitleLab.bottom+12, kAppW()-48, 1))];
        _leadwebView.backgroundColor = UIColor.clearColor;
        _leadwebView.scrollView.backgroundColor = UIColor.clearColor;
        _leadwebView.opaque = NO;
        [_botView addSubview:_leadwebView];
        
        
        
        
        UIButton *agreementBtn = [JMControl createButtonWithFrame:(CGRectMake(0, _leadTitleLab.bottom+32, kAppW(), 60)) title:@"《Política de privacidad》" titleColor:Hex_Str_COLOR(@"#222222") titleFont:Font(12) backgroundColor:UIColor.clearColor Target:self Action:@selector(agreementBtnClick)];
        [_botView addSubview:agreementBtn];
        

        if (htmlString.length) {
            NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
            [self.leadwebView loadHTMLString:htmlString baseURL:nil];
            CGFloat h = calculateYYLabelHeight(attributedString, CGSizeMake(kAppW()-30, MAXFLOAT));
            self.leadwebView.height = h;
            self.leadwebView.top = _leadTitleLab.bottom+12;
            agreementBtn.top = self.leadwebView.bottom +32;
            
            _botView.height = agreementBtn.bottom+29+JF_BOTTOM_SPACE();
            self.height = _botView.bottom;
            NSLog(@"%.f",self.height);
        }else{
            agreementBtn.top = _leadTitleLab.bottom+32;

            _botView.height = agreementBtn.bottom+29+JF_BOTTOM_SPACE();
            self.height = _botView.bottom;
            NSLog(@"%.f",self.height);
        }
      
    }
    
    return _botView;
}
-(void)copyBtnClick{
    UIPasteboard *past = [UIPasteboard generalPasteboard];
    past.string = self.paymentModel.currentModel.paymentCode;;
    [JMProgressHelper toastInWindowWithMessage:@"Copia exitosa"];
}
-(void)agreementBtnClick{
    
}

@end
